package com.fy.fyzf.utils;

import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;

import java.lang.reflect.Field;

public class TabLayoutUtils {
    public static void setTabLayoutWidth(int left, int right, TabLayout tabLayout, Activity Activity) {
        try {
            Field slidingTabIndicatorField;
            Field textViewField;
            // 拿到tabLayout的slidingTabIndicator属性
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                slidingTabIndicatorField = tabLayout.getClass().getDeclaredField("slidingTabIndicator");

            } else {
                slidingTabIndicatorField = tabLayout.getClass().getDeclaredField("mTabStrip");
            }

            slidingTabIndicatorField.setAccessible(true);
            LinearLayout mTabStrip = (LinearLayout) slidingTabIndicatorField.get(tabLayout);
            for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                View tabView = mTabStrip.getChildAt(i);
                //拿到tabView的mTextView属性
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    textViewField = tabView.getClass().getDeclaredField("textView");
                } else {
                    textViewField = tabLayout.getClass().getDeclaredField("mTextView");
                }
                textViewField.setAccessible(true);
                TextView mTextView = (TextView) textViewField.get(tabView);
                tabView.setPadding(0, 0, 0, 0);
                // 因为想要的效果是字多宽线就多宽，所以测量mTextView的宽度
                int width = mTextView.getWidth();
                if (width == 0) {
                    mTextView.measure(0, 0);
                    width = mTextView.getMeasuredWidth();
                }
                // 设置tab左右间距,注意这里不能使用Padding,因为源码中线的宽度是根据tabView的宽度来设置的
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                params.width = width;
                params.leftMargin = left;
                params.rightMargin = right;
                tabView.setLayoutParams(params);
                tabView.invalidate();
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }


    public static View getTabView(TabLayout tabLayout,int index){
        View tabView = null;
        TabLayout.Tab tab =tabLayout.getTabAt(index);
        Field view = null;
        try {
            view = TabLayout.Tab.class.getDeclaredField("mView");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        view.setAccessible(true);
        try {
            tabView = (View) view.get(tab);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return tabView;
    }
}
